﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Imaginecup.shareData;
using System.Diagnostics;

namespace Imaginecup.util
{
    public class SoundMan
    {
        private Dictionary<String , Song> m_BackGroundSoundMap = new Dictionary<string,Song>();
        private Dictionary<string, SoundEffect> m_EffectMap = new Dictionary<string, SoundEffect>();
        private ContentManager m_Content;


        private static SoundMan soundMan = null;


        private SoundMan()
        {
            m_Content = new ContentManager(StateShareData.GetShareData().GetGame().Services);
            m_Content.RootDirectory = "Content";
        }

        public static SoundMan GetSoundMan(){
            if( soundMan == null ){
                soundMan = new SoundMan();

            }
            return soundMan;
        }

        public SoundEffect GetSoundEffect(String key)
        {            
            if (m_EffectMap.ContainsKey(key))
                return m_EffectMap[key];
            
            return null;
        }


        public bool IsContainSong( String key )
        {
            if( m_BackGroundSoundMap.ContainsKey(key))
            {
                return true;
            }
            return false;
        }
        public void LoadSong(String key , String fileName)
        {
            try
            {
                SoundMan.GetSoundMan().SetSong(key, m_Content.Load<Song>(fileName));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
        public void LoadEffect(String key , String fileName)
        {
            try
            {
                SoundMan.GetSoundMan().SetEffect(key, m_Content.Load<SoundEffect>(fileName));
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }

        public void PlayBackSound(String songName)
        {
            if (!m_BackGroundSoundMap.ContainsKey(songName))
            {
                return;
            }

            try
            {
                MediaPlayer.Stop();
                Song song = m_BackGroundSoundMap[songName];
                MediaPlayer.IsRepeating = true;
              
                MediaPlayer.Play(song);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }

           
        }

        public void stopBackSound(){
            try{
                MediaPlayer.Stop();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }

        public void playEffect(String effectName)
        {
            if (!m_EffectMap.ContainsKey(effectName))
            {
                return;
            }

            try{
                SoundEffect effect = m_EffectMap[effectName];
                effect.Play(SoundEffect.MasterVolume, 0.0f, 0.0f);
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }

        private void SetSong(String key , Song song)
        {
            if (!m_BackGroundSoundMap.ContainsKey(key))
            {
                m_BackGroundSoundMap.Add(key, song);
            }
        }

        private void SetEffect(String key, SoundEffect effect)
        {
            if (!m_EffectMap.ContainsKey(key))
            {
                m_EffectMap.Add(key, effect);
            }            
        }

        
        public void ReleaseSound( ){
            try{
                MediaPlayer.Stop();
            }
            finally { }

            foreach( KeyValuePair<String, Song> pair in m_BackGroundSoundMap)
            {
                pair.Value.Dispose();
            }

            m_BackGroundSoundMap.Clear();

            foreach (KeyValuePair<String, SoundEffect> pair in m_EffectMap)
            {
                pair.Value.Dispose();
            }
            m_EffectMap.Clear();

            m_Content.Unload();
        }

        public void SetMusicVolume(double volume)
        {
        //    MediaPlayer.Pause();
            MediaPlayer.Volume = (float)volume;
         //   MediaPlayer.Resume();
        }

        public void SetEffectVolume(double volume)
        {
            SoundEffect.MasterVolume = (float)volume;
           // playEffect("DefaultEffect");
        }

        public float GetEffectVolume()
        {
            return SoundEffect.MasterVolume;
        }

        public float GetMusicVolume()
        {
            return MediaPlayer.Volume;
        }
    }
}
